/*
 * @lc app=leetcode.cn id=917 lang=javascript
 *
 * [917] 仅仅反转字母
 */

// @lc code=start
/**
 * @param {string} s
 * @return {string}
 */
const isAlphabet = (ch) => {
  return /^[a-zA-Z]$/.test(ch);
}
var reverseOnlyLetters = function (s) {
  let arr = s.split('');
  let start = 0, end = arr.length - 1;
  while (start < end) {
    while (start < end && !isAlphabet(arr[start])) start++;
    while (start < end && !isAlphabet(arr[end])) end--;
    if (start >= end) break;
    const temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    start++;
    end--;
  }
  return arr.join('');
};
// @lc code=end

